<?php
class updatercurl {
    const HANYAT = 5;
    //const SLEEP = 500;
    const TIMEOUT = 30;

    private $counter;
    private $step;
    private $handles;
    private $opts;
    private $results;

    function __construct($url = false, $sajatIndex = false, $egybol = false) {
        $this->reset();

        $this->setOpts(array(
            CURLOPT_TIMEOUT => self::TIMEOUT,
            CURLOPT_HEADER => 0,
            CURLOPT_RETURNTRANSFER => true
        ));

        if ($url !== false) {
            if (is_array($url)) {
                $this->setUrls($url);
            } else {
                $this->set($url, $sajatIndex);
            }
        }

        if ($egybol) {
            $this->start();
        }
    }

    public function reset()
    {
        $this->handles = array();
        $this->results = array();
        $this->counter = 0;
        $this->step = 1;
    }

    public function addUrl($url, $sajatIndex = false) {
        $init = curl_init();

        $this->setOpts(array(
            CURLOPT_URL => $url,
        ));

        foreach ($this->opts as $opt => $val) {
            curl_setopt($init, $opt, $val);
        }

        if ($sajatIndex === false)
            $sajatIndex = $this->counter;
        $this->handles[$this->counter++] = array('handle' => $init,
                                                 'index'  => $sajatIndex);
        return $this;
    }

    public function setOpt($opt, $value)
    {
        $this->opts[$opt] = $value;
    }

    public function setOpts($opts)
    {
        foreach ($opts as $opt => $value) {
            $this->setOpt($opt, $value);
        }
    }

    public function addUrls(array $urls) {
        foreach($urls as $key => $url) {
            $this->addUrl($url, $key);
        }
    }

    public function start() {
        if (empty($this->handles)) {
            return false;
        }

        $mh = curl_multi_init();

        $jelen = $this->step * self::HANYAT;
        $meddig = $this->counter < $jelen ? $this->counter : $jelen;
        $mettol = ($this->step-1) * self::HANYAT;

        for ($i = $mettol; $i < $meddig; $i++) {
            curl_multi_add_handle($mh, $this->handles[$i]['handle']);
        }

        $running = null;
        do {
            while(curl_multi_exec($mh, $running) == CURLM_CALL_MULTI_PERFORM);

            while($done = curl_multi_info_read($mh)) {
                for ($i = $mettol; $i < $meddig; $i++) {
                    if ($this->handles[$i]['handle'] == $done['handle']) {
                        $this->results[$this->handles[$i]['index']] = curl_multi_getcontent($done['handle']);
                        curl_multi_remove_handle($mh, $done['handle']);
                    }
                }
            }
            curl_multi_select($mh);
        } while($running > 0);
        curl_multi_close($mh);

        if ($meddig < $this->counter) {
            $this->step++;
            $this->start();
        }
        return true;

    }

    public function getResult($index = false) {
        if (empty($this->results)) {
            $this->start();
        }

        if ($index !== false) {
            return $this->results[$index];
        }
        return $this->results;
    }
}